package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CreditAndDepositCardBean;
import cn.rongdao.jrkabao.bean.deposit.SelectCollectionCardBean;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2018/1/22
 */

public class ItemCreditAndBankCardUIComponent extends LinearLayout {
    public ItemCreditAndBankCardUIComponent(Context context) {
        super(context);
    }

    public ItemCreditAndBankCardUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemCreditAndBankCardUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivAvatar;
    private TextView tvBankName;
    private TextView tvBankNumber;
    private TextView tvDelete;
    private TextView tvName;
    private ImageView ivImage;
    private TextView tvSetting;
    private LinearLayout llMain;
    private ImageView ivCompletion;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        tvSetting = findViewById(R.id.ic_tv_setting);
        ivAvatar = findViewById(R.id.ic_iv_avatar);
        tvBankName = findViewById(R.id.ic_tv_bank_name);
        tvBankNumber = findViewById(R.id.ic_tv_bank_number);
        tvDelete = findViewById(R.id.ic_tv_delete);
        tvName = findViewById(R.id.ic_tv_name);
        llMain = findViewById(R.id.ic_ll_main);
        ivImage = findViewById(R.id.ic_iv_image);
        ivCompletion = findViewById(R.id.ic_iv_completion);
    }

    public void setContent(CreditAndDepositCardBean data) {
        llMain.setVisibility(View.VISIBLE);
        tvName.setText(FRString.valueOf(data.getName()));
        tvBankName.setText(FRString.valueOf(data.getBank_name()));
        tvBankNumber.setText(CommonUtil.getFormatBankCard(data.getBank_number()));
        FRImageLoad.loadImage(FRString.valueOf(data.getBank_icon()), ivAvatar, R.drawable.icon_no_distinguish_bank);
        tvSetting.setGravity(Gravity.LEFT);
        tvDelete.setVisibility(View.VISIBLE);
        if (!FRString.isEmpty(data.getIs_default()) && data.getIs_default().equals("1")) {
            ivImage.setBackgroundResource(R.drawable.default_bank_card_bg);
            tvSetting.setText("主卡");
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.default_circle_bg);
            drawable.setBounds(0, 0, FRDisplayMetrics.dp2px(getContext(), 10.0f), FRDisplayMetrics.dp2px(getContext(), 10.0f));
            tvSetting.setCompoundDrawables(drawable, null, null, null);
        } else {
            ivImage.setBackgroundResource(R.drawable.other_bank_card_bg);
            tvSetting.setText("设为主卡");
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.other_circle_bg);
            drawable.setBounds(0, 0, FRDisplayMetrics.dp2px(getContext(), 10.0f), FRDisplayMetrics.dp2px(getContext(), 10.0f));
            tvSetting.setCompoundDrawables(drawable, null, null, null);
        }
        ivCompletion.setImageResource(CommonUtil.isStatusSuccess(data.getStatus()) ? R.drawable.icon_completion : R.drawable.icon_not_completion);
    }

    public void setContent(SelectCollectionCardBean data) {
        llMain.setVisibility(View.VISIBLE);
        tvBankName.setText(FRString.valueOf(data.getBank_name()));
        tvBankNumber.setText(CommonUtil.getFormatBankCard(data.getBank_number()));
        tvName.setText("信用卡取现收款卡");
        FRImageLoad.loadImage(FRString.valueOf(data.getBank_icon()), ivAvatar, R.drawable.icon_no_distinguish_bank);
        if (data.isSelected()) {
            ivImage.setBackgroundResource(R.drawable.default_bank_card_bg);
        } else {
            ivImage.setBackgroundResource(R.drawable.other_bank_card_bg);
        }
    }

    public void setOnDeleteListener(OnClickListener onDelectListener) {
        tvDelete.setOnClickListener(onDelectListener);
    }

    public void setOnSettingListener(OnClickListener onSettingListener) {
        tvSetting.setOnClickListener(onSettingListener);
    }
}